<template>
  <div class="list">
    <ul>
      <li class="item"
        v-for="item of letters"
        :key="item"
        :ref="item"
        @touchStrat="handleTouchStrat"
        @touchMove="handleTouchMove"
        @touchEnd="handleTouchEnd"
        @click="handleLetterClick"
      >
        {{item}}
      </li>
      <li class="item"  @click="handleLetterClick">A</li>
      <li class="item" @click="handleLetterClick">B</li>
      <li class="item" @click="handleLetterClick">A</li>
      <li class="item" @click="handleLetterClick">A</li>
      <li class="item" @click="handleLetterClick">A</li>
      <li class="item" @click="handleLetterClick">A</li>
      <li class="item" @click="handleLetterClick">A</li>
    </ul>
  </div>

</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  // 构建letters数组
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  data () {
    return {
      touchStatus: false
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleTouchStrat () {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        const touchStartY = this.$refs['A'][0].offsetTop
        const touchY = e.touches[0].clientY - 79
        const index = Math.floor((touchY - touchStartY) / 20)
        if (index >= 0 && index < this.letters.length) {
          this.$emit('changes', this.letters[index])
        }
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style scoped lang="stylus">
  @import '~styles/varibles.styl'
  .list
    display: flex
    flex-direction: column
    justify-content :center
    position: absolute
    top:1.58rem
    right:0
    bottom:0
    width: .4rem
    font-size: .32rem
    color: #666
    .item
      text-align: center
      line-height:.44rem
      font-size: .32rem
      color: $bgColor
</style>
